<script setup lang="ts">
import {Delete, Edit, InfoFilled} from "@element-plus/icons-vue";
import {deleteRecordAPI, getRecordAPI} from "@/api/user.ts";
import {onMounted, ref} from "vue";
import {ElMessage, ElNotification} from "element-plus";

// 获取记录列表
const recordList = ref()
const getRecord = async () => {
  const res = await getRecordAPI()
  recordList.value = res.data
}

// 删除记录
const deleteRecord = async (id) => {
  const res = await deleteRecordAPI(id)
  if (res.code === 0) {
    ElMessage({
      type: 'success',
      message: res.message
    })
  }
}

// 编辑记录
const EditRecord = () => {
  ElNotification({
    title: 'Warning',
    message: '为减缓服务器压力，暂关闭编辑接口及数据回显',
    type: 'warning',
  })
}

onMounted(() => {
  getRecord()
})
</script>

<template>
  <el-card class="box-card">
    <div class="card-body-table">
      <el-table
          border
          class="el-table-box"
          ref="multipleTableRef"
          :data="recordList"
          style="width: 100%"
      >
        <el-table-column label="#" width="60" align="center" type="index"/>
        <el-table-column align="center" label="用户">
          <template #default="{row}">
            {{ row.userId.username }}
          </template>
        </el-table-column>
        <el-table-column prop="exerciseTime" align="center" label="锻炼时间"/>
        <el-table-column prop="stepNumber" align="center" label="步数"/>
        <el-table-column prop="consume" align="center" label="消耗"/>
        <el-table-column prop="score" align="center" label="评分"/>
        <el-table-column prop="createAt" align="center" label="时间"/>
        <el-table-column align="center" label="控制" width="220">
          <template #default="{row}">
            <div class="operator-btn">
              <el-button type="primary" size="default" @click="EditRecord" :icon="Edit as any" circle/>
              <el-popconfirm
                  width="200"
                  @confirm="deleteRecord(row._id)"
                  confirm-button-text="确定"
                  cancel-button-text="取消"
                  :icon="InfoFilled as any"
                  icon-color="#f36b6c"
                  title="您确定删除此记录吗?"
              >
                <template #reference>
                  <el-button type="danger" size="default" :icon="Delete as any" circle/>
                </template>
              </el-popconfirm>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </el-card>
</template>

<style scoped lang="scss">
.box-card {
  padding: 0 50px;

  .card-body-table {
    margin-top: 20px;

    .el-table-box {
      height: calc(90vh - 110px);
    }
  }
}
</style>